from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

from extensions import login_manager,db


class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(64))

    def set_password(self, password):  # 设置密码hash
        self.password = generate_password_hash(password)

    def verify_password(self, password):  # 验证密码hash
        return check_password_hash(self.password, password)

    @login_manager.user_loader  # 是否登录验证
    def load_user(user_id):
        return User.query.get(int(user_id))

    # Required for administrative interface
    def __unicode__(self):
        return self.username
